package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParamType;
import com.sailfish.springbootdemo.pojo.db1.TechDeviceParamType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface TechDeviceParamTypeDao {

    /**
     * 根据设备类型查询
     *
     * @param pcbaDeviceTypeId
     * @return
     */
    @Results(id = "techDeviceParamTypeResultMap", value = {
            @Result(column = "tech_device_param_type_id", property = "techDeviceParamTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_type_name", property = "techDeviceParamTypeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            " select * from " + DB1TableName.TECH_DEVICE_PARAM_TYPE + " where 1 = 1 " +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (tech_device_param_type_name like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            "</script>")
    List<TechDeviceParamType> getAllTechDeviceParamTypeByDeviceType(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("searchValue") String searchValue);

    /**
     * 根据id查询
     *
     * @param techDeviceParamTypeId
     * @return
     */
    @ResultMap(value = "techDeviceParamTypeResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM_TYPE + " where tech_device_param_type_id = #{techDeviceParamTypeId}")
    TechDeviceParamType getPcbaDeviceParamTypeById(@Param("techDeviceParamTypeId") Integer techDeviceParamTypeId);

    /**
     * 判断有没有重复的
     */
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_DEVICE_PARAM_TYPE + " where tech_device_param_type_name = #{techDeviceParamTypeName} and pcba_device_type_id = #{pcbaDeviceTypeId} " +
            "<if test='techDeviceParamTypeId != null and techDeviceParamTypeId != \"\" '>" +
            " and tech_device_param_type_id != #{techDeviceParamTypeId}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "techDeviceParamTypeResultMap")
    List<TechDeviceParamType> judgeUniqueTechDeviceParamType(@Param("techDeviceParamTypeId") Integer techDeviceParamTypeId, @Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("techDeviceParamTypeName") String techDeviceParamTypeName);

    /**
     * 删除
     *
     * @param techDeviceParamTypeId
     * @return
     */
    @Delete("delete from " + DB1TableName.TECH_DEVICE_PARAM_TYPE + " where tech_device_param_type_id = #{techDeviceParamTypeId}")
    Integer deleteTechDeviceParamType(@Param("techDeviceParamTypeId") Integer techDeviceParamTypeId);

    /**
     * 新增
     *
     * @param techDeviceParamType
     * @return
     */
    @Insert("insert into " + DB1TableName.TECH_DEVICE_PARAM_TYPE + "(tech_device_param_type_name,status,sort,create_time,pcba_device_type_id) values " +
            "(#{techDeviceParamType.techDeviceParamTypeName},#{techDeviceParamType.status},#{techDeviceParamType.sort},#{techDeviceParamType.createTime},#{techDeviceParamType.pcbaDeviceTypeId})")
    Integer addTechDeviceParamType(@Param("techDeviceParamType") TechDeviceParamType techDeviceParamType);

    /**
     * 更新
     *
     * @param techDeviceParamType
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.TECH_DEVICE_PARAM_TYPE +
            "<set>" +
            "<if test='techDeviceParamType.techDeviceParamTypeName != null and techDeviceParamType.techDeviceParamTypeName != \"\" '>" +
            " tech_device_param_type_name = #{techDeviceParamType.techDeviceParamTypeName}," +
            "</if>" +
            "<if test='techDeviceParamType.sort != null and techDeviceParamType.sort != \"\" '>" +
            " sort = #{techDeviceParamType.sort}," +
            "</if>" +
            "<if test='techDeviceParamType.pcbaDeviceTypeId != null and techDeviceParamType.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{techDeviceParamType.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='techDeviceParamType.status != null and techDeviceParamType.status != \"\" '>" +
            " status = #{techDeviceParamType.status}," +
            "</if>" +
            "</set>" +
            " where tech_device_param_type_id = #{techDeviceParamType.techDeviceParamTypeId}" +
            "</script>")
    Integer updateTechDeviceParamType(@Param("techDeviceParamType") TechDeviceParamType techDeviceParamType);

    @ResultMap(value = "techDeviceParamTypeResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM_TYPE + " where pcba_device_type_id = #{pcbaDeviceTypeId} and tech_device_param_type_name = #{techDeviceParamTypeName}")
    TechDeviceParamType getByTechDeviceTypeIdAndName(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("techDeviceParamTypeName") String techDeviceParamTypeName);
}
